import Api from '../js/lib/Api'
import ApiDefine from '../js/lib/interface'
import FN from '../js/lib/fn'

let params = FN.getUrlParams();

Api.config({
	methods: 'GET', // GET POST ...
	dataType: 'jsonp', //json || jsonp
//	domain: 'http://hapi.test.3k.game/',
	domain: 'http://hapi.3k.com/',
	useMock: Number(params.useMock) || false,
//	useMock: true,
//	useMock: /:8088/.test(window.location.href),
	urlModel: 1
});


let commonData = {
	'hd_id': 1, //活动id
	'pf': FN.isAndroid, //用户设备，1安卓2IOS，识别不是ios的皆默认为1安卓
	'from_id': params.from_id, //来源ID，获取链接上面的from_id参数
};

Object.assign(commonData, {
	'ct': 'activity',
	'ac': 'index'
})

console.log(commonData);

export default {
	changeBox({ commit, state }, name) {
		let obj = {
			key: name,
			value: true
		}
		commit('INIT_BOX');
		commit('CHANGE_BOX', obj);
	},
	initBox({ commit, state }) {
		commit('INIT_BOX');
	},
	async clickCount({ commit, state }, data){
		Api.require('clickCount', Object.assign(commonData, {
			'ct': 'index',
			'ac': 'event_record',
			'event': data.id,
		}));
	},
	async activityTime({ commit, state }) {
		let res = await Api.require('activityTime', Object.assign(commonData, {
			'ct': 'activity',
			'ac': 'index'
		}));
		
		/*if(res.code == 1 && !/:8088/.test(window.location.href)){
			window.location = 'http://ptapi.3k.com/?ct=user&ac=user_oauth'+ '&callbackUrl=' + encodeURIComponent(window.location.href);
		}*/
		state.activityType = Number(!res.code);
		state.nextBeginTime = res.error;
	},
	async getQuestion({ commit, state }) {
		let res = await Api.require('getQuestion', Object.assign(commonData, {
			'ct': 'qa_system',
			'ac': 'get_q_list'
		}));
		if(res.code == 0) {
			
			state.nextBeginTime = res.data.end_msg;
			
			return res.data;
		} else {
			console.log(res.error);
			alert(res.error);
			return false;
		}
	},
	async answerQuestion({ commit, state }, data) {
		let res = await Api.require('answerQuestion', Object.assign(commonData,{
			'ct': 'qa_system',
			'ac': 'answer'
		}, data));
		if(res.code == 0) {
			return res.data;
		} else {
			console.log(res.error);
			alert(res.error);
			console.log(res)
			return false;
		}
	},
	async drawLottery() {
		let res = await Api.require('drawLottery', Object.assign(commonData, {
			'ct': 'activity',
			'ac': 'calc_prize'
		}));
		if(res.code == 0) {
			return res.data;
		} else {
			console.log(res.error);
			alert(res.error);
			history.back();
		}
	},
	async receiveLottery() {
		let res = await Api.require('receiveLottery', Object.assign(commonData, {
			'ct': 'activity',
			'ac': 'get_prize'
		}));
		if(res.code == 0) {
			return res.data;
		} else {
			console.log(res.error);
			alert(res.error);
		}
	},
	async activityAwardRecord() {
		let res = await Api.require('activityAwardRecord', Object.assign(commonData, {
			'ct': 'qa_system',
			'ac': 'get_qa_record'
		}));
		if(res.code == 0) {
			return res.data;
		} else {
			console.log(res.error);
			alert(res.error);
		}
	},
	async lotteryAwardRecord() {
		let res = await Api.require('lotteryAwardRecord', Object.assign(commonData, {
			'ct': 'activity',
			'ac': 'get_draw_record'
		}));
		if(res.code == 0) {
			return res.data;
		} else {
			console.log(res.error);
			alert(res.error);
		}
	}
}